package com.csii.mytest;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.csii.mytest.activity.Activity1;
import com.csii.mytest.service.AIDLService;
import com.csii.mytest.service.MyService;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    public static final String TAG = "MainActivity";
    @BindView(R.id.btn_activity)
    Button btnActivity;
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.btn_start_service)
    Button btnStartService;
    @BindView(R.id.btn_stop_service)
    Button btnStopService;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;
    @BindView(R.id.bind_service)
    Button bindService;
    @BindView(R.id.unbind_service)
    Button unbindService;
    @BindView(R.id.btn_br)
    Button btnBr;
    @BindView(R.id.btn_aidl)
    Button btnAIDL;
    @BindView(R.id.btn_start_aidl_service)
    Button btnStartAidlService;
    @BindView(R.id.btn_stop_aidl_service)
    Button btnStopAidlService;

    private MyService.MyBinder myBinder;

    private ServiceConnection connection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBinder = (MyService.MyBinder) service;
            myBinder.startDownload();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.button, R.id.btn_start_service, R.id.btn_stop_service,
            R.id.bind_service, R.id.unbind_service, R.id.btn_br,
            R.id.btn_start_aidl_service, R.id.btn_stop_aidl_service,
            R.id.btn_aidl, R.id.btn_activity})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_activity:
                Activity1.startAction(MainActivity.this);
                break;
            case R.id.button:
                ContentProviderActivity.startAction(MainActivity.this);
                break;
            case R.id.btn_start_service:
                Intent startIntent = new Intent(this, MyService.class);
                startService(startIntent);
                break;
            case R.id.btn_stop_service:
                Intent stopIntent = new Intent(this, MyService.class);
                stopService(stopIntent);
                break;
            case R.id.bind_service:
                Intent bindIntent = new Intent(this, MyService.class);
                bindService(bindIntent, connection, BIND_AUTO_CREATE);
                break;
            case R.id.unbind_service:
                unbindService(connection);
                break;
            case R.id.btn_br:
                BroadcastReceiverActivity.startAction(MainActivity.this);
                break;
            case R.id.btn_start_aidl_service:
                Intent startaidlIntent = new Intent(this, AIDLService.class);
                startService(startaidlIntent);
                break;
            case R.id.btn_stop_aidl_service:
                Intent stopaidlIntent = new Intent(this, AIDLService.class);
                stopService(stopaidlIntent);
                break;
            case R.id.btn_aidl:
                AIDLActivity.startAction(MainActivity.this);
                break;
            default:
                break;
        }
    }
}
